class Tool {
    /**
     * 
     * 
     * @param {number} n 
     * @param {number} m 
     * @returns 
     * 
     * @memberOf Tool
     */
    static Random(n: number, m: number) {
        return Math.floor(Math.random() * (n - m)) + m;
    }
    static RandomFloat(n: number, m: number) {
        return Math.random() * (n - m) + m;
    }

    /**当前时间,秒 */
    static TimeNow(): number {
        let time = new Date().getTime() / 1000;
        return time;
    }


    /**根据当前的时间与离开的时间做对比,得到一个表示时间的字符串
     * 刚刚,X分钟前,X小时前,X天前
     */
    static GetTimeString(time: number) {
        let str = '';
        let dec = Tool.TimeNow() - time;

        let smin = 60;
        let shour = 3600;
        let sday = shour * 24;
        let smonth = sday * 30;
        let syear = smonth * 365;
        if (dec < 60) {
            str = '刚刚';
        }
        else if (dec < shour) {
            let min = Math.floor(dec / smin);
            str = min.toFixed(0) + '分钟前';
        }
        else if (dec < sday) {
            let hour = Math.floor(dec / shour);
            str = hour.toFixed(0) + '小时前';
        }
        else if (dec < smonth) {
            let hour = Math.floor(dec / sday);
            str = hour.toFixed(0) + '天前';
        }
        else if (dec < syear) {
            let hour = Math.floor(dec / smonth);
            str = hour.toFixed(0) + '月前';
        }
        else {
            let hour = Math.floor(dec / syear);
            str = hour.toFixed(0) + '年前';
        }
        return str;
    }

    /**
     * 格式化时间戳
     */
    static TimeHours(time: number, timeoff?: boolean) {
        let timeoffx = timeoff && time > 60 * 60 * 24;
        if (timeoffx) {
            time -= 60 * 60 * (24 + 8);
        }
        let date = new Date(Number(time) * 1000);
        let day = date.getDate();
        let monthIndex = date.getMonth() + 1;
        let year = date.getFullYear();
        let minutes = date.getMinutes();
        let hours = date.getHours();
        let seconds = date.getSeconds();
        if (timeoff && !timeoffx) {
            day -= 1;
            hours -= 8;
        }
        return date;
    }

    /**
     * 格式化时间戳
     * 
     * @static
     * @param {number} time 时间
     * @param {string} [format] 格式
     * @param {boolean} [timeoff] 是否使用时长,时间是从0天开始的,非时长是从1970年1月1日8点开始
     * * @param {boolean} [onlyHours] 是否把天数换算成小时（时间是从0天开始的,30天以内，format 为 'MM:ss'）
     * @returns 
     * 
     * @memberOf GameFunc
    * */
    static TimeFormat(time: number, format?: string, timeoff?: boolean, onlyHours?: boolean) {
        if (!format) format = 'yyyy-mm-dd HH:MM:ss';

        let timeoffx = timeoff && time > 60 * 60 * 24;
        if (timeoffx) {
            time -= 60 * 60 * (24 + 8);
        }

        let date = new Date(Number(time) * 1000);
        let day = date.getDate();
        let monthIndex = date.getMonth() + 1;
        let year = date.getFullYear();
        let minutes = date.getMinutes();
        let hours = date.getHours();
        let seconds = date.getSeconds();

        if (timeoff && !timeoffx) {
            day -= 1;
            hours -= 8;
        }

        if (hours < 0) {
            hours = day * 24 + hours;
            day -= 1;
        }

        if (onlyHours) {
            format = 'MM:ss';
            format = format.replace('MM', this.NumberToFixed2(minutes));
            format = format.replace('ss', this.NumberToFixed2(seconds));
            let allHours = day * 24 + hours;
            format = allHours + ':' + format;
        }
        else {
            format = format.replace('yyyy', year.toString());
            format = format.replace('mm', this.NumberToFixed2(monthIndex));
            format = format.replace('dd', this.NumberToFixed2(day));
            format = format.replace('HH', this.NumberToFixed2(hours));
            format = format.replace('MM', this.NumberToFixed2(minutes));
            format = format.replace('ss', this.NumberToFixed2(seconds));
        }
        // let str = '{0}-{1}-{2} {3}:{4}:{5}'.format(year.toString(),
        //     monthIndex.toString(), day.toString(), this.NumberToFixed2(hours), this.NumberToFixed2(minutes), this.NumberToFixed2(seconds));
        return format;
    }

    static NumberToFixed2(v: number): string {
        if (v >= 10) return v.toString();
        return '0' + v.toString();
    }

    /**
     * 时间差(毫秒)
     */
    static TimeOffset(time1: number, time2: number) {
        let off = (Number(time1) * 1000) - (Number(time2) * 1000);
        return off;
    }

    /**
     * 获得设置的时间点的下个时间点星期x的x点
     * 比如当天是星期2，需要星期1的3点，则会返回下个星期1的3点
     * 
     * @param {number} t 当前时间/设置的时间点
     * @param {number} nextWeek 星期几(1-7)
     * @param {number} nextHouse 几点(0-23)
     * @returns {Date} 返回时间点
     * 
     * @memberOf GameFunc
     */
    static getTimeForNextTime(t: number, nextWeek: number, nextHouse: number): Date {
        let date = new Date(t);
        //剩余时间更新显示
        //获得当前的时间
        let day = date.getDate();
        let week = date.getDay();
        let month = date.getMonth() + 1;
        let year = date.getFullYear();
        let minutes = date.getMinutes();
        let hours = date.getHours();
        let seconds = date.getSeconds();
        //--------
        let nextTime: number = 0;//结束时间
        let refTime: number = 0;//刷新时间
        //-下次结束时间
        let nowTimeDay = new Date(year, month - 1, day);//当天时间
        let nw = (nextWeek == 7 ? 0 : nextWeek);
        let offDay = nw - week;//刷新时间差
        offDay = offDay < 0 ? ((6 - week) + (nw + 1)) : offDay;
        //今天,但时间点在时间之后,则加上一个星期
        if (offDay == 0 && nextHouse <= hours)
            offDay += 7;

        //-
        //当前时间+差时
        let daysc = (offDay * 24 * 60 * 60 * 1000);//天数差的秒
        daysc += nextHouse * 60 * 60 * 1000;//多少点
        nextTime = nowTimeDay.getTime() + daysc;

        let time = new Date(nextTime);
        logger.i(Tool.TimeFormat(nextTime / 1000));
        return time;
    }

    /**当前操作是否在UI上 */
    static isStage(e) {
        //是否是在UI上,
        // logger.i(e.target, e.target == 'Stage');
        if (!e)
            return true;
        let is = e.target.__className == "laya.display.Stage";
        return is;
        // if (e.target as Laya.Component) {Stage
        //     return false;
        // }
        // return true;
    }

    /*
** randomWord 产生任意长度随机字母数字组合
** randomFlag-是否任意长度 min-任意长度最小位[固定位数] max-任意长度最大位
** xuanfeng 2014-08-28
*/
    static randomWord(randomFlag, min, max) {
        let str = "",
            range = min,
            arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

        // 随机产生
        if (randomFlag) {
            range = Math.round(Math.random() * (max - min)) + min;
        }
        for (let i = 0; i < range; i++) {
            let pos = Math.round(Math.random() * (arr.length - 1));
            str += arr[pos];
        }
        return str;
    }


    static isNull(str: any) {
        if (str == null || str == 0 || (str.trim && str.trim() == ''))
            return true;
        return false;
    }

    /**计算两个触摸点之间的距离*/
    static getDistance(points: Array<any>): number {
        let distance: number = 0;
        if (points && points.length == 2) {
            let dx: number = points[0].stageX - points[1].stageX;
            let dy: number = points[0].stageY - points[1].stageY;

            distance = Math.sqrt(dx * dx + dy * dy);
        }
        return distance;
    }

    static getChildAsType(obj: Laya.Node, type: any): any {
        let typeName = type.__className;
        for (let i: number = 0; i < obj.numChildren; i++) {
            let child = obj.getChildAt(i);
            let childName = (child as any).__proto__.__className;
            if (typeName == childName) {
                return child;
            }
        }
        return null;
    }
}